package com.itheima.d5_synchronized_method;

public class Ticket implements Runnable {
    private static int ticker = 100;

    @Override
    public void run() {
        while (true) {
            boolean sell = sell2();
            if (sell) {
                break;
            } else {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public synchronized boolean sell() {
        if (ticker <= 0) {
            return true;
        } else {
            ticker--;
            System.out.println(Thread.currentThread().getName() + "窗口剩余票数" + ticker);
            return false;
        }
    }

    public synchronized static boolean sell2() {
        if (ticker <= 0) {
            return true;
        } else {
            ticker--;
            System.out.println(Thread.currentThread().getName() + "窗口剩余票数" + ticker);
            return false;
        }
    }
}
